#import "ALEntry.h"
#import "ALElement.h"
#import "ALVersion.h"


@implementation ALEntry

+(NSSet *)keyPathsForValuesAffectingLabel {
	return [NSSet setWithObject:@"template.label"];
}

-(NSString *)label {
	return [[self template] label];
}

-(void)setLabel:(NSString *)label {
	[[self template] setLabel:label];
}

+(NSSet *)keyPathsForValuesAffectingPrimaryElement {
    return [NSSet setWithObject:@"template.primaryElementTemplate"];
}

-(ALElement *)primaryElement {
    NSSet *primaryElements = [[self elements] filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"isPrimary == TRUE"]];
    if ([primaryElements count] == 0)
        return nil;
    return [primaryElements anyObject];
}

-(NSDate *)date {
	NSDate *date = nil;
	for (ALElement *element in [self elements]) {
		ALElementType type = (ALElementType)[[element elementType] integerValue];
		if (type == ALElementTypeDate && (!date || [element definesEntryDate]))
			date = [[element dateValue] copy];
	}
	if (!date) {
		ALVersion *version = [self version];
		date = [version releaseDate];
	}
	return date;
}

@end
